//
//  LoginViewController.m
//  UI12豆瓣大项目
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//



#import "LoginViewController.h"
#import "RegistViewController.h"
#import "User.h"
#import "ActivitySinglton.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic,retain) NSArray *nameArray;
@property (nonatomic,retain) NSArray *passwordArray;
@end

@implementation LoginViewController
- (void)dealloc
{
    [_passwordLabel release];
    [_userLabel release];
    [_userTextField release];
    [_passwordTextField release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    
    self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 100, 200, 50)];
    self.userLabel.text = @"用户名";
    self.userLabel.font = [UIFont systemFontOfSize:21];
    [self.view addSubview:self.userLabel];
    [_userTextField release];
    
    self.userTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 100, 200, 50)];
    self.userTextField.placeholder = @"请输入用户名";
    self.userTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.userTextField];
    [self.userTextField release];
    
    self.passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 200, 50)];
    self.passwordLabel.text = @"密码";
    self.passwordLabel.font = [UIFont systemFontOfSize:21];
    [self.view addSubview:self.passwordLabel];
    [_passwordTextField release];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 200, 200, 50)];
    //隐藏密码!
    self.passwordTextField.secureTextEntry = YES;
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.passwordTextField];
    [_passwordTextField release];
    
    //登陆键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(20, 320, 150, 50);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    //注册键
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(200, 320, 150, 50);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(registButtonAtion:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
    
    //回收键盘
    _userTextField.delegate = self;
    _passwordTextField.delegate =self;

}

//回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

//读取数据
-(void)readData{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    self.nameArray = [userDefaultes arrayForKey:@"username"];  //必须NSArray
    self.passwordArray = [userDefaultes arrayForKey:@"password"];
//    NSLog(@"11111%@", self.nameArray);
//    NSLog(@"11111%@", self.passwordArray);

}

//导航左侧按钮
-(void)leftBarButtonAction:(UIBarButtonItem *)barbutton{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



//注册按钮
-(void)registButtonAtion:(UIButton *)button{
    RegistViewController *registVC = [[RegistViewController alloc] init];
    //指定代理人  并让其执行协议方法
    
    UINavigationController *registNaVC = [[UINavigationController alloc] initWithRootViewController:registVC];
    [self presentViewController:registNaVC animated:YES completion:^{
    }];
    [registVC release];
}


//登陆按钮
-(void)leftAction:(UIButton *)button2{

    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertController *alertF = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];

    [alertF addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder=@"请问出了什么问题?";
    }];

    [alertC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:^{
                        NSLog(@"返回调取页");
        }];
    }]];
    [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    
    [alertF addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [alertF addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    
//判断
    User *tempUser = [[ActivitySinglton shareDataBaseSingleton] selectUserWithTitle:self.userTextField.text];
    if ([tempUser.name isEqualToString:self.userTextField.text] && self.userTextField.text.length != 0 && [tempUser.password isEqualToString:self.passwordTextField.text] && self.passwordTextField.text.length != 0) {
    [self presentViewController:alertC animated:YES completion:^{
        
        //登陆成功后做状态保存     //!!注意位置
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"login"];
        
        //登陆成功后保存登陆状态下用户名 ,别忘了注销时候清空
        [[NSUserDefaults standardUserDefaults] setObject:self.userTextField.text forKey:@"UserName"];
        
        NSLog(@"状态保存成功");
        
    }];

    }else{
 
        [self presentViewController:alertF animated:YES completion:^{
          
        }];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
